
<template>
    
    <button @click="testRouter">测试路由</button>
    <div class="user-manager">
        <!-- 新增导航栏 -->
        <nav class="main-nav">
      <router-link to="/" class="nav-link">用户管理</router-link>
      <router-link to="/about" class="nav-link">关于</router-link>
        </nav>
        <!-- 搜索过滤模块 -->
        <div class="search-box">
            <input 
          v-model="searchQuery"
          type="text"
          placeholder="搜索用户..."
          class="search-input"
            >
            <button @click="showAddDialog" class="add-button">
            <span class="icon">+</span> 添加用户
            </button>
        </div>
  
      <!-- 用户列表 -->
      <div class="user-table">
        <div v-if="filteredUsers.length === 0" class="empty-tip">
          🚨 没有找到匹配的用户
        </div>
  
        <div v-else>
          <div class="table-header">
            <span class="header-item">ID</span>
            <span class="header-item">用户名</span>
            <span class="header-item">操作</span>
          </div>
  
          <div 
            v-for="user in filteredUsers" 
            :key="user.id"
            class="table-row"
          >
            <span class="cell">{{ user.id }}</span>
            <span class="cell">{{ user.name }}</span>
            <div class="cell">
              <button @click="editUser(user)" class="action-btn edit">编辑</button>
              <button @click="deleteUser(user.id)" class="action-btn delete">删除</button>
            </div>
          </div>
        </div>
      </div>
  
      <!-- 添加/编辑对话框 -->
      <div v-if="showDialog" class="modal-mask">
        <div class="modal-wrapper">
          <div class="modal-container">
            <h3>{{ editingUser ? '编辑用户' : '新增用户' }}</h3>
            
            <form @submit.prevent="submitForm">
              <div class="form-group">
                <label>用户名：</label>
                <input 
                  v-model="formData.name" 
                  required
                  class="form-input"
                >
              </div>
  
              <div class="button-group">
                <button type="button" @click="closeDialog" class="cancel-btn">
                  取消
                </button>
                <button type="submit" class="confirm-btn">
                  {{ editingUser ? '更新' : '创建' }}
                </button>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script>
  let nextUserId = 4
  
  export default {
    name: 'UserDashboard',
    data() {
      return {
        searchQuery: '',
        showDialog: false,
        editingUser: null,
        formData: { name: '' },
        users: [
          { id: 1, name: '张三' },
          { id: 2, name: '李四' },
          { id: 3, name: '王五' }
        ]
      }
    },
    computed: {
      filteredUsers() {
        return this.users.filter(user => 
          user.name.toLowerCase().includes(this.searchQuery.toLowerCase())
        )
      }
    },
    methods: {
      showAddDialog() {
        this.formData = { name: '' }
        this.editingUser = null
        this.showDialog = true
      },
      editUser(user) {
        this.formData = { ...user }
        this.editingUser = user
        this.showDialog = true
      },
      deleteUser(id) {
        this.users = this.users.filter(user => user.id !== id)
      },
      closeDialog() {
        this.showDialog = false
        this.formData = { name: '' }
      },
      submitForm() {
        if (this.editingUser) {
          // 更新现有用户
          const index = this.users.findIndex(u => u.id === this.editingUser.id)
          this.users.splice(index, 1, { 
            ...this.editingUser, 
            ...this.formData 
          })
        } else {
          // 添加新用户
          this.users.push({
            id: nextUserId++,
            ...this.formData
          })
        }
        this.closeDialog()
      },
      testRouter() {
      console.log('当前路由：', this.$route.path)
      console.log('路由实例：', this.$router)
    }
    }
  }
  </script>
  
  <style scoped>
  .user-manager {
    padding: 20px;
    max-width: 800px;
    margin: 0 auto;
  }
  
  .search-box {
    display: flex;
    gap: 15px;
    margin-bottom: 25px;
  }
  
  .search-input {
    flex: 1;
    padding: 8px 12px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 14px;
  }
  
  .add-button {
    background: #42b983;
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 5px;
  }
  
  .icon {
    font-weight: bold;
  }
  
  .user-table {
    border: 1px solid #eee;
    border-radius: 6px;
    overflow: hidden;
  }
  
  .table-header {
    display: grid;
    grid-template-columns: 80px 1fr 160px;
    background: #f8f9fa;
    padding: 12px;
    font-weight: bold;
    border-bottom: 1px solid #eee;
  }
  
  .table-row {
    display: grid;
    grid-template-columns: 80px 1fr 160px;
    padding: 12px;
    border-bottom: 1px solid #eee;
    transition: background 0.2s;
  }
  
  .table-row:hover {
    background: #f8f9fa;
  }
  
  .cell {
    display: flex;
    align-items: center;
  }
  
  .action-btn {
    padding: 4px 8px;
    margin: 0 4px;
    border: none;
    border-radius: 3px;
    cursor: pointer;
  }
  
  .edit {
    background: #ffc107;
    color: #333;
  }
  
  .delete {
    background: #dc3545;
    color: white;
  }
  
  .modal-mask {
    position: fixed;
    z-index: 9998;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
  }
  
  .modal-container {
    background: white;
    padding: 20px;
    border-radius: 8px;
    width: 400px;
  }
  
  .form-group {
    margin: 15px 0;
  }
  
  .form-input {
    width: 100%;
    padding: 8px;
    margin-top: 5px;
  }
  
  .button-group {
    margin-top: 20px;
    display: flex;
    gap: 10px;
    justify-content: flex-end;
  }
  
  .confirm-btn {
    background: #42b983;
    color: white;
    padding: 8px 16px;
  }
  
  .cancel-btn {
    background: #6c757d;
    color: white;
    padding: 8px 16px;
  }
  
  .empty-tip {
    text-align: center;
    padding: 30px;
    color: #666;
  }
  /* 新增导航样式 */
.main-nav {
  background: #f8f9fa;
  padding: 15px 20px;
  margin-bottom: 30px;
  border-radius: 8px;
  display: flex;
  gap: 20px;
}
.nav-link {
  color: #2c3e50;
  text-decoration: none;
  padding: 8px 12px;
  border-radius: 4px;
  transition: all 0.3s;
}
.nav-link:hover {
  background: #e9ecef;
}
.nav-link.router-link-exact-active {
  background: #42b983;
  color: white;
}
  </style>  